package cn.sdjzu.exercise;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint({ "ShowToast", "WorldReadableFiles" })
public class ControlPanelActivity extends BasedActivity implements
		OnClickListener {

	private TextView mTitleUserNm;
	private Button mPersonalInfoBtn;
	private Button mSelectModeBtn;
	private Button mLastTrainBtn;
	private Button mCusomizeBtn;
	private Button mExercisePositionTestBtn;
	private Button mMaxForceTestBtn;
	private Button mTransmissionWay;
	private Button mEdit;
	ExerciseApp app;
	String userNm;
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExerciseApp.getInstance().addActivity(this);
		setContentView(R.layout.control_panel);
		app = (ExerciseApp) ControlPanelActivity.this.getApplication();
		preferences = getSharedPreferences("itYao", Context.MODE_WORLD_READABLE);
		editor = preferences.edit();
		TransToast = Toast.makeText(getApplicationContext(),
				"连接失败,请检查串口或者重新选择通讯方式！", Toast.LENGTH_LONG);
		mTitleUserNm = (TextView) findViewById(R.id.control_panel_title_usernm);
		userNm = preferences.getString("userName", "Admin");
		mTitleUserNm.setText(userNm);
		mPersonalInfoBtn = (Button) findViewById(R.id.panel_personal_info);
		mSelectModeBtn = (Button) findViewById(R.id.panel_select_mode);
		mLastTrainBtn = (Button) findViewById(R.id.panel_last_training);
		mCusomizeBtn = (Button) findViewById(R.id.panel_mode_customize);
		mExercisePositionTestBtn = (Button) findViewById(R.id.panel_test_exercise_position);
		mMaxForceTestBtn = (Button) findViewById(R.id.panel_test_max_force);
		mTransmissionWay = (Button) findViewById(R.id.selectWay);
		mEdit = (Button) findViewById(R.id.editSelectWay);

		mPersonalInfoBtn.setOnClickListener(this);
		mSelectModeBtn.setOnClickListener(this);
		mLastTrainBtn.setOnClickListener(this);
		mCusomizeBtn.setOnClickListener(this);
		mExercisePositionTestBtn.setOnClickListener(this);
		mMaxForceTestBtn.setOnClickListener(this);
		mTransmissionWay.setOnClickListener(this);
		mEdit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.panel_personal_info:
			Intent intent1 = new Intent(this, PersonalInfoActivity.class);
			startActivity(intent1);
			this.finish();
			break;
		case R.id.panel_select_mode:
			Intent intent2 = new Intent(this, SelectModeActivity.class);
			startActivity(intent2);
			this.finish();
			break;
		case R.id.panel_last_training:
			Intent intent3 = new Intent(this, HistoryTrainingActivity.class);
			startActivity(intent3);
			this.finish();
			break;
		case R.id.panel_mode_customize:
			Intent intent4 = new Intent(this, CustomizeModeActivity.class);
			startActivity(intent4);
			this.finish();
			break;
		case R.id.panel_test_exercise_position:
			Intent intent5 = new Intent(this, TestPositionActivity.class);
			startActivity(intent5);
			this.finish();
			break;
		case R.id.panel_test_max_force:
			Intent intent6 = new Intent(this, TestMaxForceActivity.class);
			startActivity(intent6);
			this.finish();
			break;

		case R.id.selectWay:
			selectTransWayDialog();
			break;
		case R.id.editSelectWay:
//			Intent intent = new Intent(this,
//					CommandActivity.class);
//			startActivity(intent);
//			this.finish();
		    close();
			break;
		default:
			break;
		}
	}

	public void selectTransWayDialog() {
		AlertDialog.Builder selectWay = new AlertDialog.Builder(this);
		selectWay.setTitle("请配置通讯方式");
		selectWay.setIcon(android.R.drawable.ic_dialog_info);
		selectWay.setPositiveButton(R.string.selectSocket,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						editor.putString("TransmissionWay", "selectWIFI");
						editor.commit();
						Intent intent7 = new Intent(ControlPanelActivity.this,
								ConfigIPAndPORTActivity.class);
						startActivity(intent7);
						ControlPanelActivity.this.finish();
					}
				});
		selectWay.setNegativeButton(R.string.selectSerialport,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						editor.putString("TransmissionWay", "selectSerialport");
						editor.commit();
						Intent intent7 = new Intent(ControlPanelActivity.this,
								ConfigSerialPortActivity.class);
						startActivity(intent7);
						ControlPanelActivity.this.finish();
					}
				});
		selectWay.show();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		close();
	}
}
